package cn.edu.nju.software.action.category;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.CategoryShow;
import cn.edu.nju.software.bean.storage.Category;
import cn.edu.nju.software.service.intf.CategoryService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class GetCategoryMenuAction extends ActionSupport implements SessionAware {

	private int id;
	private List<CategoryShow> level1Categories;
	private Map<Integer, List<CategoryShow>> subCategories;
	private CategoryService categoryService;
	private Map<String, Object> session;

	public void setId(int id) {
		this.id = id;
	}

	public List<CategoryShow> getLevel1Categories() {
		return level1Categories;
	}

	public Map<Integer, List<CategoryShow>> getSubCategories() {
		return subCategories;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		//如果id是根分类，则显示顶级分类和二层分类（最多5个）
		if (id == Constants.ROOT_CATEOGRY_ID) {
			DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) session.get(Constants.SESSION_ROOT_CATEGORY);
			subCategories = new LinkedHashMap<Integer, List<CategoryShow>>();
			int level1childCount = rootNode.getChildCount();
			level1Categories = new ArrayList<CategoryShow>(level1childCount);
			for (int i = 0; i < level1childCount; i++) {
				DefaultMutableTreeNode level1Node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
				level1Categories.add(new CategoryShow((Category) level1Node.getUserObject(), null));

				int level2childCount = level1Node.getChildCount();
				int min = level2childCount < 5 ? level2childCount : 5;
				List<CategoryShow> level2Categories = new ArrayList<CategoryShow>(min);
				for (int j = 0; j < min; j++) {
					DefaultMutableTreeNode level2Node = (DefaultMutableTreeNode) level1Node.getChildAt(j);
					level2Categories.add(new CategoryShow((Category) level2Node.getUserObject(), null));
				}
				subCategories.put(((Category) level1Node.getUserObject()).getCategoryID(), level2Categories);
			}
			subCategories.put(Constants.ROOT_CATEOGRY_ID, level1Categories);
		//否则显示id和其子分类
		} else {
			@SuppressWarnings("unchecked")
			Map<Integer, CategoryShow> categoriesMap = (Map<Integer, CategoryShow>) session.get(Constants.SESSION_ALL_CATEGORIES);
			Category category = categoriesMap.get(id).getCategory();
			DefaultMutableTreeNode rootNode = categoryService.getSubCategories(category);
			subCategories = new LinkedHashMap<Integer, List<CategoryShow>>();
			level1Categories = new ArrayList<CategoryShow>(1);
			level1Categories.add(new CategoryShow(category, null));
			int level2childCount = rootNode.getChildCount();
			List<CategoryShow> level2Categories = new ArrayList<CategoryShow>(level2childCount);
			for (int j = 0; j < level2childCount; j++) {
				DefaultMutableTreeNode level2Node = (DefaultMutableTreeNode) rootNode.getChildAt(j);
				level2Categories.add(new CategoryShow((Category) level2Node.getUserObject(), null));
			}
			subCategories.put(id, level2Categories);
		}
		return SUCCESS;
	}
	
}
